#!/bin/bash

#  Marmoset: a student project snapshot, submission, testing and code review
#  system developed by the Univ. of Maryland, College Park
#
#  Developed as part of Jaime Spacco's Ph.D. thesis work, continuing effort led
#  by William Pugh. See http://marmoset.cs.umd.edu/
#
#  Copyright 2005 - 2011, Univ. of Maryland
#
#  Licensed under the Apache License, Version 2.0 (the "License"); you may not
#  use this file except in compliance with the License. You may obtain a copy of
#  the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations under
#  the License.

function usage() {
    echo "Usage: $0 <campus_id> <password> [-o <output_file] [-n <employee_num>] [-f <first_name>] [-l <last_name>]"
    echo "  output_file:  file to which to write the query produced by this script"
    echo "  campus_id:    the user name for your admin account (try to pick something non-obvious)"
    echo "  password:     password for the admin account within the submit server; this is different"
    echo "                than both the MySQL root account and the host user/root accounts -- select a"
    echo "                robust password that is different from the MySQL root and host user/root"
    echo "                account passwords"
    echo "  employee_num: a numeric designation for this user; this may not apply to your situation"
    echo "                (optional, defaults to '00000')"
    echo "  first name:   the admin user's first name (optional, defaults to 'admin_first_name')"
    echo "  last name:    the admin user's last name (optional, defaults to 'admin_last_name')"
    exit
}

if [ $# -lt 2 ]; then
    usage
fi

admin_user=$1
shift

admin_pword=$1
shift

admin_empno=00000
admin_fn=admin_first_name
admin_ln=admin_last_name

output_file=./my_bootstrap.sql

while [ $# -gt 0 ]
do
  case $1 in
      -h | -help | --help )
	  usage
	  exit;;
      -o )
	  shift
	  output_file=$1
	  shift;;
      -n )
	  shift
	  admin_empno=$1
	  shift;;
      -f )
	  shift
	  admin_fn=$1
	  shift;;
      -l )
	  #echo $1
	  shift
	  admin_ln=$1
	  shift;;
  esac
done

# Print the query that will create the admin user

#Note:  this query is tied to the DB schema in use by the submit server at the time of
# this script's writing; if the schema changes, this will have to be updated as well.
echo "INSERT INTO students \
VALUES (DEFAULT, '$admin_user', '$admin_empno', '$admin_fn', '$admin_ln', 'yes', \
DEFAULT, DEFAULT, DEFAULT, '$admin_pword');" > $output_file
